package org.hisp.dhis.wsclient.j2me.javarosa;

import javax.microedition.lcdui.Image;

import de.enough.polish.ui.ChoiceItem;
import de.enough.polish.ui.FilteredList;
import de.enough.polish.ui.Style;

public class FilteredListContains
    extends FilteredList
{
    public FilteredListContains( String title, int listType )
    {
        super( title, listType );
        setMode( FILTER_INDEX_OF );
    }

    public FilteredListContains( String title, int listType, ChoiceItem[] items, Style style )
    {
        super( title, listType, items, style );
        setMode( FILTER_INDEX_OF );
    }

    public FilteredListContains( String title, int listType, ChoiceItem[] items )
    {
        super( title, listType, items );
        setMode( FILTER_INDEX_OF );
    }

    public FilteredListContains( String title, int listType, String[] stringElements, Image[] imageElements, Style style )
    {
        super( title, listType, stringElements, imageElements, style );
        setMode( FILTER_INDEX_OF );
    }

    public FilteredListContains( String title, int listType, String[] stringElements, Image[] imageElements )
    {
        super( title, listType, stringElements, imageElements );
        setMode( FILTER_INDEX_OF );
    }

    public FilteredListContains( String title, int listType, Style style )
    {
        super( title, listType, style );
        setMode( FILTER_INDEX_OF );
    }

    public static final int FILTER_STARTS_WITH = 0;
    public static final int FILTER_INDEX_OF = 1;

    public void setMode( int mode )
    {
        this.filterMode = mode;
    }

    protected boolean matches( String filterText, ChoiceItem cItem, boolean checkForSelectedRadioItem )
    {
        if ( checkForSelectedRadioItem && cItem.isSelected )
        {
            return true;
        }
        else if ( this.filterMode == FILTER_STARTS_WITH )
        {
            return (cItem.getText().toLowerCase().startsWith( filterText ));
        }
        else
        {
            return (cItem.getText().toLowerCase().indexOf( filterText ) != -1);
        }
    }
}
